//
//  ForthViewController.m
//  ShareSDKDemo
//
//  Created by Caesar on 16/9/19.
//  Copyright (c) 2016年 mob.com. All rights reserved.
//

#import "ForthViewController.h"

@implementation ForthViewController{
    UIWebView *webView;
    NSUserDefaults *userDefaults;
    NSString *redirectString;
    NSString *titleString;
    NSString *summaryString;
    NSString *imageString;
    NSString *startURLString;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    userDefaults=[[NSUserDefaults alloc] init];
    NSDictionary *dataDic=[userDefaults objectForKey:@"dataDic"];
    startURLString=[NSString stringWithFormat:@"http://www.fdcanet.com/index.php?app=agent&id=%@&act=prospect&appid=1",dataDic[@"agent_id"]];
    redirectString=startURLString;
    NSLog(@"urlString:%@",redirectString);
    webView=(UIWebView *)[self.view viewWithTag:1401];
    [webView setScalesPageToFit:YES];
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:startURLString]];
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"agency_four" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:filePath];
//    request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    webView.delegate=self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 1.添加数据
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:redirectString]];
        [webView loadRequest:request];
        // 2.2秒后刷新表格UI
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        });
    }];
    [header setTitle:@"下拉可以刷新" forState:MJRefreshStateIdle];
    [header setTitle:@"松开进行刷新" forState:MJRefreshStatePulling];
    [header setTitle:@"易美居正在帮你刷新,请稍候..." forState:MJRefreshStateRefreshing];
    webView.scrollView.mj_header = header;
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    // 1.添加数据
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:redirectString]];
    [webView loadRequest:request];
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:redirectString]];
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"agency_four" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:filePath];
//    request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

//从webview中拨打电话
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url = request.URL;
    NSString *urlString=[url absoluteString];
    NSRange range1 = [urlString rangeOfString:@"act=prospect"];
    NSRange range6 = [urlString rangeOfString:@"act=house_map"];
    if(range1.length>0){
        if (![redirectString isEqualToString:urlString]) {
            [self setLeftButtonItems:NO];
            redirectString =urlString;
            NSLog(@"urlString==%@",urlString);
        }
    }else if ([urlString isEqualToString:startURLString]){
        [self setLeftButtonItems:NO];
    }else{
        [self setLeftButtonItems:YES];
//        if(range6.length<=0){
            redirectString =urlString;
//        }
    }
    return YES;
}

- (void)setLeftButtonItems:(BOOL)setImage{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    if (setImage) {
        leftButton.frame = CGRectMake(0, 0, 44, 44);
        [leftButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
        [leftButton setImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal];
    }
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)goBack:(UIButton *)button
{
    if (webView.canGoBack) {
        [webView goBack];
    }
}

@end
